from django.utils import timezone
from datetime import timedelta
from rest_framework import serializers
from device.models import warningDeviceInfo, warningSettingInfo, temperatureInfo, channelInfo, deviceInfo, \
    warningDistrictSetting


class deviceInfoSerializer(serializers.ModelSerializer):

    running_day = serializers.SerializerMethodField()

    def get_running_day(self,obj):
        days_difference = (timezone.now() - obj.create_at).days
        return days_difference

    class Meta:
        model = deviceInfo
        fields = "__all__"


class channelInfoSerializer(serializers.ModelSerializer):

    class Meta:
        model = channelInfo
        fields = "__all__"


class temperatureInfoSerializer(serializers.ModelSerializer):

    channel_name = serializers.CharField(source='belong_channel.name', read_only=True)

    class Meta:
        model = temperatureInfo
        fields = "__all__"

class warningDistrictSettingSerializer(serializers.ModelSerializer):

    class Meta:
        model = warningDistrictSetting
        fields = "__all__"


class warningSettingInfoSerializer(serializers.ModelSerializer):
    channel_name = serializers.CharField(source='belong_channel.name', read_only=True)
    total_meter = serializers.CharField(source='belong_channel.total_meter', read_only=True)
    total_warning = serializers.SerializerMethodField()
    running_day = serializers.SerializerMethodField()
    district_info = serializers.SerializerMethodField(help_text='水质参数详情')

    def get_district_info(self, obj):
        if obj.all_district_details:
            return obj.all_district_details.all().values('id', 'name', 'meter_start', 'meter_end', 'temperature_info')
        else:
            return []

    def get_total_warning(self,obj):
        warningObj = warningDeviceInfo.objects.filter(belong_channel=obj.belong_channel).count()
        return warningObj

    def get_running_day(self,obj):
        warningObj = warningDeviceInfo.objects.filter(belong_channel=obj.belong_channel).last()
        if warningObj:
            days_difference = (timezone.now() - warningObj.order_time).days
        else:
            deviceOjb = deviceInfo.objects.get(pk=obj.belong_channel.belong_device.id)
            days_difference = (timezone.now() - deviceOjb.create_at).days
        return days_difference

    class Meta:
        model = warningSettingInfo
        fields = "__all__"


class warningDeviceInfoSerializer(serializers.ModelSerializer):
    channel_name = serializers.CharField(source='belong_channel.name', read_only=True)
    is_warning_play = serializers.CharField(source='belong_channel.is_warning_play', read_only=True)
    warning_name = serializers.SerializerMethodField()
    district_info = serializers.SerializerMethodField()


    def get_warning_name(self, obj):
        if obj.warning_type == 'constant':
            return '定温报警'
        elif obj.warning_type == 'different':
            return '差温报警'
        else:
            return '断纤报警'

    def get_district_info(self, obj):
        district_name = ''
        warningObj = warningSettingInfo.objects.get(belong_channel=obj.belong_channel)
        for item in warningObj.all_district_details.all():
            if item.meter_start <= obj.meter_address <= item.meter_end:
                district_name = item.name
                break
        return district_name

    class Meta:
        model = warningDeviceInfo
        fields = "__all__"